﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Mathmall.Administration.Migrations
{
    /// <inheritdoc />
    public partial class InitialCreate : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Attr",
                columns: table => new
                {
                    AttrID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    AttrName = table.Column<string>(type: "char(30)", nullable: true, comment: "属性名"),
                    SearchType = table.Column<byte>(type: "tinyint", nullable: true, comment: "是否需要检索[0-不需要，1-需要]"),
                    Icon = table.Column<string>(type: "varchar(255)", nullable: true, comment: "属性图标"),
                    ValueSelect = table.Column<string>(type: "char(255)", nullable: true, comment: "可选值列表[用逗号分隔]"),
                    AttrType = table.Column<byte>(type: "tinyint", nullable: true, comment: "属性类型[0-销售属性，1-基本属性，2-既是销售属性又是基本属性]"),
                    Enable = table.Column<byte>(type: "tinyint", nullable: true, comment: "可选值列表[用逗号分隔]"),
                    CatalogID = table.Column<byte>(type: "tinyint", nullable: true, comment: "所属分类"),
                    ShowDescription = table.Column<byte>(type: "tinyint", nullable: true, comment: "快速展示【是否展示在介绍上；0-否 1-是】，在sku中仍然可以调整"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "创建时间"),
                    LastUpdateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("AttrID", x => x.AttrID);
                },
                comment: "商品属性");

            migrationBuilder.CreateTable(
                name: "AttrAttrgroupRelation",
                columns: table => new
                {
                    AttrAttrgroupRelationID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    AttrID = table.Column<int>(type: "int", nullable: true, comment: "属性id"),
                    AttrGroupID = table.Column<int>(type: "int", nullable: true, comment: "属性分组id"),
                    AttrSort = table.Column<int>(type: "int", nullable: true, comment: "属性组内排序"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "创建时间"),
                    LastUpdateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("AttrAttrgroupRelationID", x => x.AttrAttrgroupRelationID);
                },
                comment: "属性&属性分组关联");

            migrationBuilder.CreateTable(
                name: "AttrGroup",
                columns: table => new
                {
                    AttrGroupID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    AttrGroupName = table.Column<string>(type: "char(20)", nullable: true, comment: "组名"),
                    Sort = table.Column<int>(type: "int", nullable: true, comment: "排序"),
                    Description = table.Column<string>(type: "varchar(255)", nullable: true, comment: "描述"),
                    Icon = table.Column<string>(type: "varchar(255)", nullable: true, comment: "组图标"),
                    CatalogID = table.Column<int>(type: "int", nullable: true, comment: "所属分类id"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "创建时间"),
                    LastUpdateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("AttrGroupID", x => x.AttrGroupID);
                },
                comment: "属性分组");

            migrationBuilder.CreateTable(
                name: "Brand",
                columns: table => new
                {
                    BrandID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "char(50)", nullable: true, comment: "品牌名"),
                    Logo = table.Column<string>(type: "varchar(2000)", nullable: true, comment: "品牌logo地址"),
                    Description = table.Column<string>(type: "ntext", nullable: true, comment: "介绍"),
                    ShowStatus = table.Column<byte>(type: "tinyint", nullable: true, comment: "显示状态[0-不显示；1-显示]"),
                    FirstLetter = table.Column<string>(type: "char(1)", nullable: true, comment: "检索首字母"),
                    Sort = table.Column<int>(type: "int", nullable: true, comment: "排序"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "创建时间"),
                    LastUpdateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("BrandID", x => x.BrandID);
                },
                comment: "品牌");

            migrationBuilder.CreateTable(
                name: "Category",
                columns: table => new
                {
                    CategoryID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "char", nullable: true, comment: "分类名称"),
                    ParentCID = table.Column<int>(type: "int", nullable: true, comment: "父分类id"),
                    CatLevel = table.Column<int>(type: "int", nullable: true, comment: "层级"),
                    ShowStatus = table.Column<byte>(type: "tinyint", nullable: true, comment: "是否显示[0-不显示，1显示]"),
                    Sort = table.Column<int>(type: "int", nullable: true, comment: "顺序"),
                    Icon = table.Column<string>(type: "char(255)", nullable: true, comment: "图标地址"),
                    ProductUnit = table.Column<string>(type: "char(50)", nullable: true, comment: "计量单位"),
                    ProductCount = table.Column<int>(type: "int", nullable: true, comment: "商品数量"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "创建时间"),
                    LastUpdateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("CategoryID", x => x.CategoryID);
                },
                comment: "商品三级分类");

            migrationBuilder.CreateTable(
                name: "CategoryBrandRelation",
                columns: table => new
                {
                    CategoryBrandRelationID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    BrandID = table.Column<int>(type: "int", nullable: true, comment: "品牌id"),
                    CategoryID = table.Column<int>(type: "int", nullable: true, comment: "分类id"),
                    BrandName = table.Column<string>(type: "varchar(255)", nullable: true, comment: "品牌名称"),
                    CategoryName = table.Column<string>(type: "varchar(255)", nullable: true, comment: "分类名称"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "创建时间"),
                    LastUpdateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("CategoryBrandRelationID", x => x.CategoryBrandRelationID);
                },
                comment: "品牌分类关联");

            migrationBuilder.CreateTable(
                name: "CommentReplay",
                columns: table => new
                {
                    CommentReplayID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CommentID = table.Column<int>(type: "int", nullable: true, comment: "评论id"),
                    ReplayID = table.Column<int>(type: "int", nullable: true, comment: "品牌id"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "创建时间"),
                    LastUpdateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("CommentReplayID", x => x.CommentReplayID);
                },
                comment: "商品评价回复关系");

            migrationBuilder.CreateTable(
                name: "ProductAttrValue",
                columns: table => new
                {
                    ProductAttrValueID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    SpuID = table.Column<int>(type: "int", nullable: true, comment: "商品id"),
                    AttrID = table.Column<int>(type: "int", nullable: true, comment: "属性id"),
                    AttrName = table.Column<string>(type: "varchar(200)", nullable: true, comment: "属性名"),
                    AttrValue = table.Column<string>(type: "varchar(200)", nullable: true, comment: "属性值"),
                    AttrSort = table.Column<int>(type: "int", nullable: true, comment: "顺序"),
                    QuickShow = table.Column<byte>(type: "tinyint", nullable: true, comment: "快速展示【是否展示在介绍上；0-否 1-是】"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "创建时间"),
                    LastUpdateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("ProductAttrValueID", x => x.ProductAttrValueID);
                },
                comment: "spu属性值");

            migrationBuilder.CreateTable(
                name: "SkuImage",
                columns: table => new
                {
                    SkuImageID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    SkuID = table.Column<int>(type: "int", nullable: true, comment: "skuid"),
                    ImageUrl = table.Column<string>(type: "varchar(255)", nullable: true, comment: "图片地址"),
                    ImageSort = table.Column<int>(type: "int", nullable: true, comment: "排序"),
                    DefaultImg = table.Column<int>(type: "int", nullable: true, comment: "默认图[0 - 不是默认图，1 - 是默认图]"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "创建时间"),
                    LastUpdateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("SkuImageID", x => x.SkuImageID);
                },
                comment: "sku图片");

            migrationBuilder.CreateTable(
                name: "SkuInfo",
                columns: table => new
                {
                    SkuInfoID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    SpuID = table.Column<int>(type: "int", nullable: true, comment: "SpuID"),
                    SkuName = table.Column<string>(type: "varchar(255)", nullable: true, comment: "Sku名称"),
                    SkuDescription = table.Column<string>(type: "varchar(2000)", nullable: true, comment: "sku介绍描述"),
                    CatalogID = table.Column<int>(type: "int", nullable: true, comment: "所属分类id"),
                    BrandID = table.Column<int>(type: "int", nullable: true, comment: "品牌id"),
                    SkuDefaultImage = table.Column<string>(type: "varchar(255)", nullable: true, comment: "默认图片"),
                    SkuTitle = table.Column<string>(type: "varchar(255)", nullable: true, comment: "标题"),
                    SkuSubtitle = table.Column<string>(type: "varchar(2000)", nullable: true, comment: "副标题"),
                    Price = table.Column<decimal>(type: "money", nullable: true, comment: "价格"),
                    SaleCount = table.Column<int>(type: "int", nullable: true, comment: "销量"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "创建时间"),
                    LastUpdateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("SkuInfoID", x => x.SkuInfoID);
                },
                comment: "sku信息");

            migrationBuilder.CreateTable(
                name: "SkuSaleAttrValue",
                columns: table => new
                {
                    SkuSaleAttrValueID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    SkuID = table.Column<int>(type: "int", nullable: true, comment: "SkuID"),
                    AttrID = table.Column<int>(type: "int", nullable: true, comment: "AttrId"),
                    AttrName = table.Column<string>(type: "varchar(200)", nullable: true, comment: "销售属性名"),
                    AttrValue = table.Column<string>(type: "varchar(200)", nullable: true, comment: "销售属性值"),
                    AttrSort = table.Column<int>(type: "int", nullable: true, comment: "排序"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "创建时间"),
                    LastUpdateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("SkuSaleAttrValueID", x => x.SkuSaleAttrValueID);
                },
                comment: "sku销售属性&值");

            migrationBuilder.CreateTable(
                name: "SpuComment",
                columns: table => new
                {
                    SpuCommentID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    SkuID = table.Column<int>(type: "int", nullable: true, comment: "SkuID"),
                    SpuID = table.Column<int>(type: "int", nullable: true, comment: "SpuID"),
                    SpuName = table.Column<string>(type: "varchar(255)", nullable: true, comment: "商品名字"),
                    MemberNickName = table.Column<string>(type: "varchar(255)", nullable: true, comment: "会员昵称"),
                    Star = table.Column<byte>(type: "tinyint", nullable: true, comment: "星级"),
                    MemberIp = table.Column<string>(type: "varchar(64)", nullable: true, comment: "会员ip"),
                    ShowStatus = table.Column<byte>(type: "tinyint", nullable: true, comment: "显示状态[0-不显示，1-显示]"),
                    SpuAttrbutes = table.Column<string>(type: "varchar(64)", nullable: true, comment: "会员ip"),
                    LikesCount = table.Column<int>(type: "int", nullable: true, comment: "点赞数"),
                    ReplayCount = table.Column<int>(type: "int", nullable: true, comment: "回复数"),
                    Resources = table.Column<string>(type: "varchar(1000)", nullable: true, comment: "评论图片/视频[json数据；[{type:文件类型,url:资源路径}]]"),
                    Content = table.Column<string>(type: "text", nullable: true, comment: "内容"),
                    MemberIcon = table.Column<string>(type: "varchar(255)", nullable: true, comment: "用户头像"),
                    CommentType = table.Column<byte>(type: "tinyint", nullable: true, comment: "评论类型[0 - 对商品的直接评论，1 - 对评论的回复]"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "创建时间"),
                    LastUpdateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("SpuCommentID", x => x.SpuCommentID);
                },
                comment: "商品评价");

            migrationBuilder.CreateTable(
                name: "SpuImage",
                columns: table => new
                {
                    SpuImageID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    SpuID = table.Column<int>(type: "int", nullable: true, comment: "图片名"),
                    ImageName = table.Column<string>(type: "varchar(200)", nullable: true, comment: "图片名"),
                    ImageUrl = table.Column<string>(type: "varchar(255)", nullable: true, comment: "图片地址"),
                    ImageSort = table.Column<int>(type: "int", nullable: true, comment: "顺序"),
                    DefaultImage = table.Column<byte>(type: "tinyint", nullable: true, comment: "是否默认图"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "创建时间"),
                    LastUpdateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("SpuImageID", x => x.SpuImageID);
                },
                comment: "spu图片");

            migrationBuilder.CreateTable(
                name: "SpuInfo",
                columns: table => new
                {
                    SpuInfoID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    SpuName = table.Column<string>(type: "varchar(200)", nullable: true, comment: "商品名称"),
                    SpuDescription = table.Column<string>(type: "varchar(1000)", nullable: true, comment: "商品描述"),
                    CatalogID = table.Column<int>(type: "int", nullable: true, comment: "所属分类id"),
                    BrandID = table.Column<int>(type: "int", nullable: true, comment: "品牌id"),
                    Weight = table.Column<decimal>(type: "decimal(18,0)", nullable: true, comment: "商品重量"),
                    PublishStatus = table.Column<byte>(type: "tinyint", nullable: true, comment: "上架状态[0 - 下架，1 - 上架]"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "创建时间"),
                    LastUpdateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("SpuInfoID", x => x.SpuInfoID);
                },
                comment: "spu信息");

            migrationBuilder.CreateTable(
                name: "SpuInfoDescription",
                columns: table => new
                {
                    SpuInfoDescriptionID = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Description = table.Column<string>(type: "text", nullable: true, comment: "商品介绍"),
                    CreateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "创建时间"),
                    LastUpdateTime = table.Column<DateTime>(type: "datetime", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("SpuInfoDescriptionID", x => x.SpuInfoDescriptionID);
                },
                comment: "spu信息介绍");
            // 创建CreateTime和LastUpdateTime字段触发器，在插入和更新日期时自动更新日期
            migrationBuilder.Sql(
                @"
                    if exists(select * from sys.triggers where name = 'trg_AttrCreateTime')
                    BEGIN
	                    drop trigger trg_AttrCreateTime
                    END
                    GO
                    CREATE TRIGGER trg_AttrCreateTime
                    ON Attr
                    AFTER INSERT
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE Attr
	                    SET CreateTime = GETDATE()
	                    WHERE AttrID IN (SELECT AttrId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_AttrLastUpdateTime')
                    BEGIN
	                    drop trigger trg_AttrLastUpdateTime
                    END
                    GO
                    CREATE TRIGGER trg_AttrLastUpdateTime
                    ON Attr
                    AFTER UPDATE
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE Attr
	                    SET LastUpdateTime = GETDATE()
	                    WHERE AttrID IN (SELECT AttrId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_AttrAttrgroupRelationCreateTime')
                    BEGIN
	                    drop trigger trg_AttrAttrgroupRelationCreateTime
                    END
                    GO
                    CREATE TRIGGER trg_AttrAttrgroupRelationCreateTime
                    ON AttrAttrgroupRelation
                    AFTER INSERT
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE AttrAttrgroupRelation
	                    SET CreateTime = GETDATE()
	                    WHERE AttrAttrgroupRelationID IN (SELECT AttrAttrgroupRelationId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_AttrAttrgroupRelationLastUpdateTime')
                    BEGIN
	                    drop trigger trg_AttrAttrgroupRelationLastUpdateTime
                    END
                    GO
                    CREATE TRIGGER trg_AttrAttrgroupRelationLastUpdateTime
                    ON AttrAttrgroupRelation
                    AFTER UPDATE
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE AttrAttrgroupRelation
	                    SET LastUpdateTime = GETDATE()
	                    WHERE AttrAttrgroupRelationID IN (SELECT AttrAttrgroupRelationId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_AttrGroupCreateTime')
                    BEGIN
	                    drop trigger trg_AttrGroupCreateTime
                    END
                    GO
                    CREATE TRIGGER trg_AttrGroupCreateTime
                    ON AttrGroup
                    AFTER INSERT
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE AttrGroup
	                    SET CreateTime = GETDATE()
	                    WHERE AttrGroupID IN (SELECT AttrGroupId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_AttrGroupLastUpdateTime')
                    BEGIN
	                    drop trigger trg_AttrGroupLastUpdateTime
                    END
                    GO
                    CREATE TRIGGER trg_AttrGroupLastUpdateTime
                    ON AttrGroup
                    AFTER UPDATE
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE AttrGroup
	                    SET LastUpdateTime = GETDATE()
	                    WHERE AttrGroupID IN (SELECT AttrGroupId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_BrandCreateTime')
                    BEGIN
	                    drop trigger trg_BrandCreateTime
                    END
                    GO
                    CREATE TRIGGER trg_BrandCreateTime
                    ON Brand
                    AFTER INSERT
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE Brand
	                    SET CreateTime = GETDATE()
	                    WHERE BrandID IN (SELECT BrandId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_BrandLastUpdateTime')
                    BEGIN
	                    drop trigger trg_BrandLastUpdateTime
                    END
                    GO
                    CREATE TRIGGER trg_BrandLastUpdateTime
                    ON Brand
                    AFTER UPDATE
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE Brand
	                    SET LastUpdateTime = GETDATE()
	                    WHERE BrandID IN (SELECT BrandId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_CategoryCreateTime')
                    BEGIN
	                    drop trigger trg_CategoryCreateTime
                    END
                    GO
                    CREATE TRIGGER trg_CategoryCreateTime
                    ON Category
                    AFTER INSERT
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE Category
	                    SET CreateTime = GETDATE()
	                    WHERE CategoryID IN (SELECT CategoryId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_CategoryLastUpdateTime')
                    BEGIN
	                    drop trigger trg_CategoryLastUpdateTime
                    END
                    GO
                    CREATE TRIGGER trg_CategoryLastUpdateTime
                    ON Category
                    AFTER UPDATE
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE Category
	                    SET LastUpdateTime = GETDATE()
	                    WHERE CategoryID IN (SELECT CategoryId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_CategoryBrandRelationCreateTime')
                    BEGIN
	                    drop trigger trg_CategoryBrandRelationCreateTime
                    END
                    GO
                    CREATE TRIGGER trg_CategoryBrandRelationCreateTime
                    ON CategoryBrandRelation
                    AFTER INSERT
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE CategoryBrandRelation
	                    SET CreateTime = GETDATE()
	                    WHERE CategoryBrandRelationID IN (SELECT CategoryBrandRelationId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_CategoryBrandRelationLastUpdateTime')
                    BEGIN
	                    drop trigger trg_CategoryBrandRelationLastUpdateTime
                    END
                    GO
                    CREATE TRIGGER trg_CategoryBrandRelationLastUpdateTime
                    ON CategoryBrandRelation
                    AFTER UPDATE
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE CategoryBrandRelation
	                    SET LastUpdateTime = GETDATE()
	                    WHERE CategoryBrandRelationID IN (SELECT CategoryBrandRelationId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_CommentReplayCreateTime')
                    BEGIN
	                    drop trigger trg_CommentReplayCreateTime
                    END
                    GO
                    CREATE TRIGGER trg_CommentReplayCreateTime
                    ON CommentReplay
                    AFTER INSERT
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE CommentReplay
	                    SET CreateTime = GETDATE()
	                    WHERE CommentReplayID IN (SELECT CommentReplayId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_CommentReplayLastUpdateTime')
                    BEGIN
	                    drop trigger trg_CommentReplayLastUpdateTime
                    END
                    GO
                    CREATE TRIGGER trg_CommentReplayLastUpdateTime
                    ON CommentReplay
                    AFTER UPDATE
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE CommentReplay
	                    SET LastUpdateTime = GETDATE()
	                    WHERE CommentReplayID IN (SELECT CommentReplayId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_ProductAttrValueCreateTime')
                    BEGIN
	                    drop trigger trg_ProductAttrValueCreateTime
                    END
                    GO
                    CREATE TRIGGER trg_ProductAttrValueCreateTime
                    ON ProductAttrValue
                    AFTER INSERT
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE ProductAttrValue
	                    SET CreateTime = GETDATE()
	                    WHERE ProductAttrValueID IN (SELECT ProductAttrValueId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_ProductAttrValueLastUpdateTime')
                    BEGIN
	                    drop trigger trg_ProductAttrValueLastUpdateTime
                    END
                    GO
                    CREATE TRIGGER trg_ProductAttrValueLastUpdateTime
                    ON ProductAttrValue
                    AFTER UPDATE
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE ProductAttrValue
	                    SET LastUpdateTime = GETDATE()
	                    WHERE ProductAttrValueID IN (SELECT ProductAttrValueId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_SkuImageCreateTime')
                    BEGIN
	                    drop trigger trg_SkuImageCreateTime
                    END
                    GO
                    CREATE TRIGGER trg_SkuImageCreateTime
                    ON SkuImage
                    AFTER INSERT
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE SkuImage
	                    SET CreateTime = GETDATE()
	                    WHERE SkuImageID IN (SELECT SkuImageId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_SkuImageLastUpdateTime')
                    BEGIN
	                    drop trigger trg_SkuImageLastUpdateTime
                    END
                    GO
                    CREATE TRIGGER trg_SkuImageLastUpdateTime
                    ON SkuImage
                    AFTER UPDATE
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE SkuImage
	                    SET LastUpdateTime = GETDATE()
	                    WHERE SkuImageID IN (SELECT SkuImageId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_SkuInfoCreateTime')
                    BEGIN
	                    drop trigger trg_SkuInfoCreateTime
                    END
                    GO
                    CREATE TRIGGER trg_SkuInfoCreateTime
                    ON SkuInfo
                    AFTER INSERT
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE SkuInfo
	                    SET CreateTime = GETDATE()
	                    WHERE SkuInfoID IN (SELECT SkuInfoId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_SkuInfoLastUpdateTime')
                    BEGIN
	                    drop trigger trg_SkuInfoLastUpdateTime
                    END
                    GO
                    CREATE TRIGGER trg_SkuInfoLastUpdateTime
                    ON SkuInfo
                    AFTER UPDATE
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE SkuInfo
	                    SET LastUpdateTime = GETDATE()
	                    WHERE SkuInfoID IN (SELECT SkuInfoId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_SkuSaleAttrValueCreateTime')
                    BEGIN
	                    drop trigger trg_SkuSaleAttrValueCreateTime
                    END
                    GO
                    CREATE TRIGGER trg_SkuSaleAttrValueCreateTime
                    ON SkuSaleAttrValue
                    AFTER INSERT
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE SkuSaleAttrValue
	                    SET CreateTime = GETDATE()
	                    WHERE SkuSaleAttrValueID IN (SELECT SkuSaleAttrValueId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_SkuSaleAttrValueLastUpdateTime')
                    BEGIN
	                    drop trigger trg_SkuSaleAttrValueLastUpdateTime
                    END
                    GO
                    CREATE TRIGGER trg_SkuSaleAttrValueLastUpdateTime
                    ON SkuSaleAttrValue
                    AFTER UPDATE
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE SkuSaleAttrValue
	                    SET LastUpdateTime = GETDATE()
	                    WHERE SkuSaleAttrValueID IN (SELECT SkuSaleAttrValueId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_SpuCommentCreateTime')
                    BEGIN
	                    drop trigger trg_SpuCommentCreateTime
                    END
                    GO
                    CREATE TRIGGER trg_SpuCommentCreateTime
                    ON SpuComment
                    AFTER INSERT
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE SpuComment
	                    SET CreateTime = GETDATE()
	                    WHERE SpuCommentID IN (SELECT SpuCommentId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_SpuCommentLastUpdateTime')
                    BEGIN
	                    drop trigger trg_SpuCommentLastUpdateTime
                    END
                    GO
                    CREATE TRIGGER trg_SpuCommentLastUpdateTime
                    ON SpuComment
                    AFTER UPDATE
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE SpuComment
	                    SET LastUpdateTime = GETDATE()
	                    WHERE SpuCommentID IN (SELECT SpuCommentId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_SpuImageCreateTime')
                    BEGIN
	                    drop trigger trg_SpuImageCreateTime
                    END
                    GO
                    CREATE TRIGGER trg_SpuImageCreateTime
                    ON SpuImage
                    AFTER INSERT
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE SpuImage
	                    SET CreateTime = GETDATE()
	                    WHERE SpuImageID IN (SELECT SpuImageId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_SpuImageLastUpdateTime')
                    BEGIN
	                    drop trigger trg_SpuImageLastUpdateTime
                    END
                    GO
                    CREATE TRIGGER trg_SpuImageLastUpdateTime
                    ON SpuImage
                    AFTER UPDATE
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE SpuImage
	                    SET LastUpdateTime = GETDATE()
	                    WHERE SpuImageID IN (SELECT SpuImageId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_SpuInfoCreateTime')
                    BEGIN
	                    drop trigger trg_SpuInfoCreateTime
                    END
                    GO
                    CREATE TRIGGER trg_SpuInfoCreateTime
                    ON SpuInfo
                    AFTER INSERT
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE SpuInfo
	                    SET CreateTime = GETDATE()
	                    WHERE SpuInfoID IN (SELECT SpuInfoId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_SpuInfoLastUpdateTime')
                    BEGIN
	                    drop trigger trg_SpuInfoLastUpdateTime
                    END
                    GO
                    CREATE TRIGGER trg_SpuInfoLastUpdateTime
                    ON SpuInfo
                    AFTER UPDATE
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE SpuInfo
	                    SET LastUpdateTime = GETDATE()
	                    WHERE SpuInfoID IN (SELECT SpuInfoId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_SpuInfoDescriptionCreateTime')
                    BEGIN
	                    drop trigger trg_SpuInfoDescriptionCreateTime
                    END
                    GO
                    CREATE TRIGGER trg_SpuInfoDescriptionCreateTime
                    ON SpuInfoDescription
                    AFTER INSERT
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE SpuInfoDescription
	                    SET CreateTime = GETDATE()
	                    WHERE SpuInfoDescriptionID IN (SELECT SpuInfoDescriptionId FROM inserted);
                    END;

                    if exists(select * from sys.triggers where name = 'trg_SpuInfoDescriptionLastUpdateTime')
                    BEGIN
	                    drop trigger trg_SpuInfoDescriptionLastUpdateTime
                    END
                    GO
                    CREATE TRIGGER trg_SpuInfoDescriptionLastUpdateTime
                    ON SpuInfoDescription
                    AFTER UPDATE
                    AS
                    BEGIN
	                    SET NOCOUNT ON;
	                    UPDATE SpuInfoDescription
	                    SET LastUpdateTime = GETDATE()
	                    WHERE SpuInfoDescriptionID IN (SELECT SpuInfoDescriptionId FROM inserted);
                    END;
                ");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Attr");

            migrationBuilder.DropTable(
                name: "AttrAttrgroupRelation");

            migrationBuilder.DropTable(
                name: "AttrGroup");

            migrationBuilder.DropTable(
                name: "Brand");

            migrationBuilder.DropTable(
                name: "Category");

            migrationBuilder.DropTable(
                name: "CategoryBrandRelation");

            migrationBuilder.DropTable(
                name: "CommentReplay");

            migrationBuilder.DropTable(
                name: "ProductAttrValue");

            migrationBuilder.DropTable(
                name: "SkuImage");

            migrationBuilder.DropTable(
                name: "SkuInfo");

            migrationBuilder.DropTable(
                name: "SkuSaleAttrValue");

            migrationBuilder.DropTable(
                name: "SpuComment");

            migrationBuilder.DropTable(
                name: "SpuImage");

            migrationBuilder.DropTable(
                name: "SpuInfo");

            migrationBuilder.DropTable(
                name: "SpuInfoDescription");
        }
    }
}
